package luke4fun.wheresmymoney.adapter.list.impl;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.list.BasicAdapter;
import luke4fun.wheresmymoney.bean.AccBean;
import luke4fun.wheresmymoney.bean.AccDtlBean;
import luke4fun.wheresmymoney.viewholder.AccListViewHolder;

public class DayAccAdapter extends BasicAdapter<AccBean> {

	private List<AccBean> dataList = new ArrayList<AccBean>();
	private Context context;
	private LayoutInflater layoutInflater;
	
	public DayAccAdapter(Context context) {
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AccListViewHolder holder;
		
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.acc_list, null);
			holder = new AccListViewHolder();
			holder.category = (TextView) convertView.findViewById(R.id.acc_col_category);
			holder.sum = (TextView) convertView.findViewById(R.id.acc_col_sum);
			holder.accDtlList = (LinearLayout) convertView.findViewById(R.id.acc_dtl_list);
			
			convertView.setTag(holder);
		} else {
			holder = (AccListViewHolder) convertView.getTag();
		}
		
		holder.accDtlList.removeAllViews();
		// set text view content
		AccBean acc = dataList.get(position);
		if (acc != null) {
			for (Iterator<AccDtlBean> it=acc.getDtlList().iterator(); it.hasNext();) {
				
				AccDtlBean dtlBean = it.next();
				for (AccDtlBean dtl : dtlBean.getInfoList()) {
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
					TextView item = new TextView(context);
					item.setTextAppearance(context, R.style.normalFont);
					item.setText(dtl.getItem());
					item.setLayoutParams(params);
					
					LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
					TextView price = new TextView(context);
					price.setGravity(Gravity.RIGHT);
					price.setTextAppearance(context, R.style.normalFont);
					if (dtl.getPrice() != null) {
						price.setText(String.valueOf(dtl.getPrice()));
					}
					price.setLayoutParams(params2);
					
					LinearLayout lLayout = new LinearLayout(context);
					lLayout.setOrientation(LinearLayout.HORIZONTAL);
					lLayout.addView(item);
					lLayout.addView(price);
					
					holder.accDtlList.addView(lLayout);
				}
				
			}
			
			if (acc.getCategory() != null && acc.getCategory().length() > 0) {
				holder.category.setText(acc.getCategory());
			}
			holder.sum.setText(String.valueOf( acc.getSum() ));
		}
		
		return convertView;
	}

	@Override
	public void updateList(List<AccBean> list) {
		dataList.clear();
		dataList.addAll(list);
		notifyDataSetChanged();
	}

}
